package me.jiatao.wan.ui.main.project

import android.widget.Toast
import androidx.lifecycle.*
import androidx.paging.PagingData
import androidx.paging.cachedIn
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.*
import me.jiatao.wan.AppConfig
import me.jiatao.wan.common.ui.LoadingLottieAnimation
import me.jiatao.wan.data.network.HttpConst
import me.jiatao.wan.data.network.HttpStatusResult
import me.jiatao.wan.data.network.doFailure
import me.jiatao.wan.data.network.doSuccess
import me.jiatao.wan.data.network.pojo.CategoryTreePojo
import me.jiatao.wan.data.network.pojo.NavTreePojo
import me.jiatao.wan.data.repository.Repository
import me.jiatao.wan.init.AppHelper
import me.jiatao.wan.model.ArticleModel
import me.jiatao.wan.ui.main.system.CategoryTreeAdapter
import javax.inject.Inject

/**
 * @Author: taojia
 * @CreateDate: 2021/8/31 6:21 下午
 * @Version: 1.0
 * @Description:
 */

@HiltViewModel
class ProjectViewModel @Inject constructor(
    private val repository: Repository
) : ViewModel() {

    /**
     * 设置动画加载状态
     */
    val loadingState = MutableLiveData<LoadingLottieAnimation.State>()


    /****************************** 项目模块 ******************************/

    private val fetchProjectFlow: Flow<HttpStatusResult<List<CategoryTreePojo>>> by lazy {
        repository.fetchProjectTree().shareIn(viewModelScope, SharingStarted.WhileSubscribed(AppConfig.TIME_WHILE_SUBSCRIBED), 1)
    }


    /**
     * 获取类别列表
     */
    fun fetchProjectTreeVM() = liveData {
        fetchProjectFlow
            .onStart {
                loadingState.value = LoadingLottieAnimation.State.LOADING
            }
            .collectLatest { result ->
                result.doFailure { apiEx ->
                    loadingState.value = LoadingLottieAnimation.State.ERROR

                    val msg = apiEx.message ?: HttpConst.MSG_OPERATION_FAILURE
                    Toast.makeText(AppHelper.mContext, msg, Toast.LENGTH_LONG).show()
                }
                result.doSuccess { value ->
                    emit(value)
                    loadingState.value = LoadingLottieAnimation.State.COMPLETE
                }
            }
    }


    /**
     * 加载分类下文章数据
     */
    fun loadProjectArticleListVM(cid: Int): LiveData<PagingData<ArticleModel>> =
        repository.fetchProjectArticleList(cid).cachedIn(viewModelScope).asLiveData()


    /****************************** 体系模块 ******************************/

    /**
     * 获取类别列表
     */

    private val fetchCategoryTreeFlow: Flow<HttpStatusResult<List<CategoryTreePojo>>> by lazy {
        repository.fetchCategoryTree().shareIn(viewModelScope, SharingStarted.WhileSubscribed(AppConfig.TIME_WHILE_SUBSCRIBED), 1)
    }


    var categoryTreeAdapter: CategoryTreeAdapter? = null

    fun fetchCategoryTreeVM() = liveData {
        fetchCategoryTreeFlow
            .onStart {
                loadingState.value = LoadingLottieAnimation.State.LOADING
            }
            .collectLatest { result ->
                result.doFailure { apiEx ->
                    loadingState.value = LoadingLottieAnimation.State.ERROR

                    val msg = apiEx.message ?: HttpConst.MSG_OPERATION_FAILURE
                    Toast.makeText(AppHelper.mContext, msg, Toast.LENGTH_LONG).show()
                }
                result.doSuccess { value ->
                    emit(value)
                    loadingState.value = LoadingLottieAnimation.State.COMPLETE
                }
            }
    }

    /**
     * 获取常用网址列表
     */

    private val fetchNavTreeFlow: Flow<HttpStatusResult<List<NavTreePojo>>> by lazy {
        repository.fetchNavTree().shareIn(viewModelScope, SharingStarted.WhileSubscribed(AppConfig.TIME_WHILE_SUBSCRIBED), 1)
    }


    fun fetchNavTreeVM() = liveData {
        fetchNavTreeFlow
            .onStart {
                loadingState.value = LoadingLottieAnimation.State.LOADING
            }
            .collectLatest { result ->
                result.doFailure { apiEx ->
                    loadingState.value = LoadingLottieAnimation.State.ERROR

                    val msg = apiEx.message ?: HttpConst.MSG_OPERATION_FAILURE
                    Toast.makeText(AppHelper.mContext, msg, Toast.LENGTH_LONG).show()
                }
                result.doSuccess { value ->
                    emit(value)
                    loadingState.value = LoadingLottieAnimation.State.COMPLETE
                }
            }
    }
}